package com.lxyk.service.user.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.dao.monthly.CourseDao;
import com.lxyk.dao.monthly.CourseInfoDao;
import com.lxyk.dao.user.ClassesDao;
import com.lxyk.dao.user.UserInfoDao;
import com.lxyk.domain.*;
import com.lxyk.service.user.ClassesService;
import com.lxyk.vo.StudentNumVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ClassesServiceImpl implements ClassesService {

    @Autowired
    private ClassesDao classesDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private CourseInfoDao courseInfoDao;

    @Override
    public PageBean<Classes> findAll(Integer page,Integer size) {
        //当前页数,从0开始; 每页条数
        PageHelper.startPage(page,size);
        List<Classes> list = classesDao.findAll();
        //根据班级id查询班级退费人数
        for (Classes classes : list) {
            Integer outNum = classesDao.findOutNum(classes.getId());
            classes.setOutNumber(outNum);
        }

        PageInfo<Classes> pageInfo = new PageInfo<>(list);
        PageBean<Classes> pageBean = new PageBean<>(pageInfo, list);
        return pageBean;
    }

    @Override
    public Map<String, Object> toAddTeacherPage(Integer id) {
        //查询出所有用户
        List<UserInfo> allUsers = userInfoDao.findAll();
        //根据班级id查询出班级对应的用户id
        List<Integer> myUsers = classesDao.findTeacherByCid(id);
        //创建map集合封装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("allUsers",allUsers);
        map.put("myUsers",myUsers);
        return map;
    }

    //修改班级的用户(先清空, 再往用户角色表中插入数据)
    @Override
    public void addUserToClasses(Integer[] ids, Integer cid) {
        //根据班级id清空班级对应的所有用户
        classesDao.removeByCid(cid);
        //插入新的关系数据
        for (Integer id : ids) {
            classesDao.add(id,cid);
        }
    }

    //根据id查询
    @Override
    public Classes findById(Integer id) {
        return classesDao.findById(id);
    }

    //更新班级
    @Override
    public boolean update(Classes classes) {
        return classesDao.update(classes)==1;
    }

    //删除班级
    @Override
    public boolean delete(Integer id) {
        //删除班级与用户之间的关系
        classesDao.removeByCid(id);
        //删除班级课程
        courseInfoDao.deleteByCid(id);
        //删除班级信息
        boolean flag1 = classesDao.delete(id)==1;

        return flag1;
    }

    //添加班级信息,同时生成班级课程信息
    @Override
    public boolean save(Classes classes) throws ParseException {
        classes.setClassSize(0);
        classes.setEffeNumber(0);
        //保存班级
        boolean flag1 = classesDao.save(classes)==1;
        //根据课程大纲生成班级课表信息
        //查询课程大纲
        List<Course> coursesList = courseDao.selectAll();
        int sort = 1; //排序字段
        //生成班级课表
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.YEAR,+1);
        Date date = instance.getTime();
        for (Course course : coursesList) {
            String name = course.getName();
            Integer cid = classes.getId();
            CourseInfo courseInfo = new CourseInfo(null,name,sort++,0,date,cid);
            courseInfoDao.add(courseInfo);
        }
        return flag1;
    }

    //根据用户id查询对应班级
    @Override
    public List<Classes> findByUser(Integer uid) {
        List<Classes> list = classesDao.findByUser(uid);
        //根据班级id查询班级退费人数
        for (Classes classes : list) {
            Integer outNum = classesDao.findOutNum(classes.getId());
            classes.setOutNumber(outNum);
        }
        return list;
    }

    //查询各个在读班级本周报名人数与退费人数
    @Override
    public List<StudentNumVo> findJoinOutNum() throws ParseException {
        //创建集合对象
        ArrayList<StudentNumVo> list = new ArrayList<>();
        //查询所有在读班级
        List<Classes> allStart = classesDao.findAllStart();
        for (Classes classes : allStart) {
            //根据班级id查询本周退费人数
            Integer outNum = classesDao.findOutNumWeek(classes.getId());
            //根据班级id查询本周进班人数
            Integer joinNum = classesDao.findJoinNumWeek(classes.getId());
            //创建StudentNumVo对象
            StudentNumVo studentNumVo = new StudentNumVo(classes.getId(), classes.getClassName(), joinNum, outNum);
            list.add(studentNumVo);
        }
        return list;
    }

    //查询所有在读班级
    @Override
    public List<Classes> findReadClass() {
        return classesDao.findAllStart();
    }

    //学员进班
    @Override
    public void joinClass(Integer cid){
        classesDao.editTotalNum(cid,1);
        classesDao.editEffeNum(cid,1);
    }

    //学员转班
    @Override
    public void transferClass(Integer oldCid, Integer targetCid) {
        classesDao.editTotalNum(oldCid,-1);
        classesDao.editEffeNum(oldCid,-1);
        classesDao.editTotalNum(targetCid,1);
        classesDao.editEffeNum(targetCid,1);
    }

    //学员退费
    @Override
    public void outClass(Integer cid) {
        classesDao.editTotalNum(cid,-1);
        classesDao.editEffeNum(cid,-1);
    }

    //休学,修改有效人数
    @Override
    public void suspension(Integer cid) {
        classesDao.editEffeNum(cid,-1);
    }

    //复学,修改有效人数
    @Override
    public void goBackClass(Integer cid) {
        classesDao.editEffeNum(cid,1);
    }

    @Override
    public void endClasses(Integer cid) {
        classesDao.endClasses(cid);
    }


}
